<template>
  <div o-Container>
    <o-list v-bind:source="list.user" v-bind="{modified:modify,exported:output}"/>
    <o-dialog v-bind:source="panel" v-bind="{opened:load,closed:load}">
      <template slot="content">
        <o-item v-bind:source="item.user" v-bind="{saved:save,backed:back,loaded:load}"></o-item>
      </template>
    </o-dialog>
  </div>
</template>

<script>
  export default {
    name: "mms_platform_user_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
    data() {
      return {
        octopus: this.$parent.octopus,
        //列表 数据
        list: {
          user: {
            data: {
              id: 1,
              cellData: null,
            },
          },
        },
        // 创建页面和修改页面的需要的数据
        item: {
          user: {
            data: {
              id: 1,
              value: "",
              cellData: null,
            }
          },
        },
        panel: {
          visible: false,
        },
      }
    },
    created: function () {
      this.init();
    },
    methods: {
      // 适配 平台 和 用户
      init: function () {
        let url = this.octopus.vue.$route.path;
        // 平台账户/教师
        if (url.indexOf("Organization/Staff/List") > 0 || url.indexOf("/Campus/Teacher/List") > 0) {
          this.list.user.data.cellData = [
            {name: "CompanyID", searchKey: "=", width: "100", filterKey: "="},//公司
            {name: "DepartmentID", searchKey: "=", width: "100", link: [{binding: "CompanyID", mapping: "CompanyID"}]},//根据公司筛选
            {name: "RoleID", searchKey: "=", width: "100", link: [{binding: "DepartmentID", mapping: "DepartmentID"}]},//根据部门筛选
            {name: "Name", searchKey: "LIKE", width: "100"},//账户
            {name: "NickName", searchKey: "LIKE", width: "100"},//昵称
            {name: "RealName", searchKey: "LIKE", width: "100"},//真实名称
            {name: "SensorID", width: 130, searchKey: "=", show: false},
            {name: "Mobile"},//手机号
            {name: "Mail"},//邮箱
            {name: "TypeID", show: false, filterKey: "=", filterValue: "1"},//邮箱
            {name: "LanguageTypeID"},//语言类型
            {name: "ThemeID", width: 60},//主题
            {name: "Status", width: 60, searchKey: "="},//状态
            {name: "CreateUserID"},//创建者
            {name: "CreateDate", width: 180}//创建时间
          ];
          this.list.user.button = [{class: "success", click: this.create, text: "新增"}];

          this.item.user.data.cellData = [
            {name: "CompanyID"},//公司
            {name: "DepartmentID", link: [{binding: "CompanyID", mapping: "CompanyID"}]},//
            {name: "RoleID", link: [{binding: "DepartmentID", mapping: "DepartmentID"}]},//
            {name: "Code"},
            {name: "Name"},//     名称
            {name: "NickName"},// 名称
            {name: "RealName"},// 真实名称
            {name: "Password"},// 密码
            {name: "Sex"},//      性别
            {name: "Mobile"},//   手机号
            {name: "Mail"},//     邮箱
            {name: "Height"},//   身高
            {name: "Weight"},//   体重
            {name: "SensorID"},// 传感器
            {name: "LanguageTypeID"},//
            {name: "Address"},//  地址
            {name: "IdentityNumber"},
            {name: "Birthday"},// 生日
            {name: "EmergencyContact"},
            {name: "EmergencyContactMobile"},
            {name: "ThemeID"},//  主题
            {name: "Status"},//
            // {name: "IsDelete"},//
            {name: "CreateUserID"},//创建者
            {name: "CreateDate"},//
            {name: "PhotoPath"}
          ];

          // 如果是教室端，则进行展示。
          if (url.indexOf("/Campus/Teacher/List") > 0) {
            this.octopus.tools.parseCell(this.list.user.data, "SensorID").show = true;
            this.octopus.tools.parseCell(this.item.user.data, "SensorID").show = true;
            this.item.user.button = [{class: "warning", click: this.updateTeacherInfo, text: "更新教师设备信息"}];
          }


          // 如果是 MDM 则对公司进行隐藏；
          if (url.indexOf("MDM") > 0) {
            this.octopus.tools.parseCell(this.list.user.data, "CompanyID").searchKey = null;
            this.octopus.tools.parseCell(this.list.user.data, "CompanyID").filterValue = this.octopus.passport.authorization.company;
            this.octopus.tools.parseCell(this.list.user.data, "DepartmentID").link = [{
              mapping: "CompanyID",
              value: this.octopus.passport.authorization.company
            }];


            // this.octopus.tools.parseCell(this.item.user.data, "CompanyID").defaultValue = this.octopus.passport.authorization.company+"";
            // this.octopus.tools.parseCell(this.item.user.data, "CompanyID").disabled = true;
            // 采用 组织-公司表（视图），将ID 变更为 CompanyID来适配Link功能，后续需要修复。
            this.octopus.tools.parseCell(this.item.user.data, "CompanyID").link = [{
              mapping: "CompanyID",
              value: this.octopus.passport.authorization.company
            }];
          }
        }
        // 会员/家长
        else if (url.indexOf("Organization/User/List") > 0 || url.indexOf("/Campus/Parent/List") > 0) {
          this.list.user.data.cellData = [
            {name: "CompanyID", searchKey: "=", filterKey: "="},//公司
            // {name: "Code", searchKey: "LIKE", width: 200},//名称
            {name: "Name", searchKey: "LIKE", width: 100},//名称
            {name: "NickName"},//名称
            {name: "RealName", searchKey: "LIKE", width: "100"},//真实名称
            {name: "IdentityNumber", width: 160, searchKey: "LIKE"},
            {name: "Sex", width: 40},
            {name: "Mobile", searchKey: "LIKE"},//手机号
            {name: "Address", width: 300, searchKey: "LIKE"},
            {name: "Birthday", width: 100},
            {name: "WeChatOpenID", width: 200},
            // {name: "WeChatUnionCode", width: 200},
            {name: "AliPayUserID", width: 150},
            {name: "WeChatBalance", width: 70},
            {name: "AliPayBalance", width: 70},
            {name: "Balance", width: 70},
            {name: "Status", width: 70},
            {name: "TypeID", show: false, filterKey: "=", filterValue: "2"},//用户类型
            {name: "CreateDate", width: 160}//创建时间
          ];
          this.list.user.data.orderData = "CreateDate DESC";

          this.item.user.data.cellData = [
            {name: "CompanyID"},//公司
            {name: "DepartmentID", link: [{binding: "CompanyID", mapping: "CompanyID"}]},
            {name: "RoleID", link: [{binding: "DepartmentID", mapping: "DepartmentID"}]},
            {name: "Name", disabled: true},//账号
            {name: "NickName"},//昵称
            {name: "RealName"},//真实名称
            // {name: "Password"},//密码
            {name: "Age"},//年龄
            {name: "Sex"},//性别
            {name: "Profession"},//职业
            {name: "IdentityNumber"},
            {name: "Birthday"},//
            {name: "Height"},
            {name: "Weight"},
            {name: "EmergencyContact"},
            {name: "EmergencyContactMobile"},
            {name: "Mobile"},//手机号
            {name: "Status"},//
            {name: "WeChatOpenID"},//主题
            // {name: "WeChatUnionCode"},//
            {name: "AliPayUserID"},//
            {name: "WeChatBalance"},//
            {name: "AliPayBalance"},//
            {name: "Balance"},//
            // {name: "CreateUserID"},//创建者
            {name: "CreateDate"},//
            {name: "Address"},
            {name: "PhotoPath"}//
          ];

          // 如果是 MDM 则对公司进行隐藏；
          if (url.indexOf("MDM") > 0) {
            this.octopus.tools.parseCell(this.list.user.data, "CompanyID").filterValue = this.octopus.passport.authorization.company;
            this.octopus.tools.parseCell(this.item.user.data, "CompanyID").show = false;
            this.octopus.tools.parseCell(this.item.user.data, "DepartmentID").show = false;
            this.octopus.tools.parseCell(this.item.user.data, "RoleID").show = false;
          }
        }
      },
      create: function () {
        this.item.user.data.value = null;
        this.item.user.control.load();
        this.panel.visible = true;
      },
      modify: function (row) {
        this.item.user.data.value = row.ID;
        this.item.user.control.load();
        this.panel.visible = true;
      },
      save: function (data) {
        this.octopus.tips.success(data);
        // 刷新list
        this.list.user.control.load();
        this.back();
      },
      load: function (data) {
        // let o = this;
        if (this.octopus.tools.IsNullOrEmpty(data.content.value)) {
          // this.octopus.tools.parseCell(this.item.user.data, "CompanyID").defaultValue = this.octopus.passport.authorization.company+"";
          // this.octopus.tools.parseCell(this.item.user.data, "CompanyID").link = [{
          //   mapping: "CompanyID",
          //   value: this.octopus.passport.authorization.company
          // }];
        }
      },
      back: function () {
        this.panel.visible = false;
      },
      output: function (data) {
        this.octopus.tips.success("下载完成。");
      },
      updateTeacherInfo: function (data) {
        this.item.user.control.save();

        // 有传感器ID才更新名称
        let o = this;
        let sensorID = this.octopus.tools.parseCell(data, "SensorID").defaultValue;
        let roleID = this.octopus.tools.parseCell(data, "RoleID").defaultValue;
        // 如果有传感器数据，则下发
        if (!this.octopus.tools.IsNullOrEmpty(sensorID) && !this.octopus.tools.IsNullOrEmpty(roleID)) {
          // 查询班级名称，准备下发用。
          this.octopus.request.api.post("/Octopus/Engine/Item", {
              id: 10,
              type: "SELECT",
              value: roleID,
              cellData: [
                {name: "Name"},// 岗位名称
              ]
            },
            // 处理回调事件，通过分析data.status.code的状态来实现。
            function (roleData) {
              let roleName = o.octopus.tools.parseCell(roleData.content, "Name").defaultValue;
              // 更新教师姓名并下发
              let teacherInfo = {
                "sensorID": sensorID,
                "type": "CLASSAndNAME",
                "data": {
                  "className": roleName,
                  "studentName": o.octopus.tools.parseCell(data, "RealName").defaultValue,
                }
              };
              // 下发
              o.octopus.tools.openAPI("/ESB/Sensor/Wear/Data/Push", teacherInfo, function (sendData) {
                // o.octopus.tips.debug(JSON.stringify(sendData));
                if (sendData.status.code == 200) {
                  o.octopus.tips.success("教师信息已更新下发至手环。");
                } else {
                  o.octopus.tips.error(sendData);
                }
              });
            }
          );

        } else {
          this.octopus.tips.error("未绑定传感器，无法执行发送。");
        }
      },
    },
    props: ["oData"]
  }
</script>
